
import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus';
import router from '@/router'

let baseURL = 'https://xgyrl.com/';
// let baseURL = '';
const service = axios.create({
    baseURL: baseURL,
    timeout: 30000
})
//请求拦截
service.interceptors.request.use(
    config => {
        let auth = localStorage.getItem("auth") || '';
        config.headers['Authorization'] = auth
        console.log('HTTP请求拦截', config)
        if (!config.url.includes('/gy')) {

            config.url = '/gy' + config.url;
            console.log(config.url)
        }
        return config
    }
)
// 响应拦截
service.interceptors.response.use(
    res => {
        if (res.config.responseType == 'blob' && res.status == 200) {
            return res.data
        } else if (res.data && res.data.code != 10200) {
            ElMessage({
                message: res.data.message || '请求失败',
                type: 'warning'
            });
            return false
        } else {
            return res.data.data || true
        }

    }, error => {
        console.error('请求报错',error)
        return false
    }
)
const loginTimeout = () => {
    ElMessageBox("未登录或登录超时！请重新登录", "提示", {
        type: "warning",
    }).then(() => {
        router.push({ path: '/login' })
    })
}

export default service





